/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.login;

import java.io.IOException;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.ihale.database.Database;
import org.ihale.database.DatabaseEmre;
import org.ihale.satinalmaci.SatinAlmaci;
import org.ihale.tedarikci.Tedarikci;
import org.ihale.yonetici.Admin;

/**
 *
 * @author Emre
 */
@ManagedBean
@RequestScoped
public class LoginBean {

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }
    private Admin admin = new Admin();
    private Tedarikci tedarikci = new Tedarikci();
    private SatinAlmaci satinAlmaci = new SatinAlmaci();
    private boolean tedarikciAuthentication = false;
    private boolean satinAlmaciAuthentication = false;
    private boolean adminAuthentication = false;

    public void loginAdmin() throws IOException, ClassNotFoundException, SQLException {

        DatabaseEmre db = new DatabaseEmre();
        db.connect();
        admin = db.getirAdminLogin(admin.getKullaniciAdi(), admin.getSifre());
        if (!admin.getKullaniciAdi().equals("")) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/Yonetici/AdminAnaSayfa.xhtml");
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Username and/or password is wrong. Please try again !"));
        }

        db.disconnect();
    }

    public void loginTedarikci() throws ClassNotFoundException, SQLException, IOException {

        DatabaseEmre db = new DatabaseEmre();
        db.connect();
        tedarikci = db.getirTedarikciLogin(tedarikci.getKullaniciAdi(), tedarikci.getSifre());
        if (!tedarikci.getKullaniciAdi().equals("")) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/Tedarikci/TedarikciAnaSayfa.xhtml");
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Username and/or password is wrong. Please try again !"));
        }

        db.disconnect();
    }

    public void loginSatinAlmaci() throws ClassNotFoundException, SQLException, IOException {

        DatabaseEmre db = new DatabaseEmre();
        db.connect();
        satinAlmaci = db.getirSatinAlmacilogin(satinAlmaci.getKullaniciAdi(),satinAlmaci.getSifre());
        if (!satinAlmaci.getKullaniciAdi().equals("")) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/SatinAlmaci/SatinAlmaciAnaSayfa.xhtml");
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Username and/or password is wrong. Please try again !"));
        }

        db.disconnect();
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public SatinAlmaci getSatinAlmaci() {
        return satinAlmaci;
    }

    public void setSatinAlmaci(SatinAlmaci satinAlmaci) {
        this.satinAlmaci = satinAlmaci;
    }

    public Tedarikci getTedarikci() {
        return tedarikci;
    }

    public void setTedarikci(Tedarikci tedarikci) {
        this.tedarikci = tedarikci;
    }

    public boolean isAdminAuthentication() {
        return adminAuthentication;
    }

    public void setAdminAuthentication(boolean adminAuthentication) {
        this.adminAuthentication = adminAuthentication;
    }

    public boolean isSatinAlmaciAuthentication() {
        return satinAlmaciAuthentication;
    }

    public void setSatinAlmaciAuthentication(boolean satinAlmaciAuthentication) {
        this.satinAlmaciAuthentication = satinAlmaciAuthentication;
    }

    public boolean isTedarikciAuthentication() {
        return tedarikciAuthentication;
    }

    public void setTedarikciAuthentication(boolean tedarikciAuthentication) {
        this.tedarikciAuthentication = tedarikciAuthentication;
    }
}
